package com.kai.prc.consummer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author kai
 * @date 2021/3/20 9:17
 * @description:
 */
public class NetClient {


    public static Object callRemoteService(String host,
                                           int port,
                                           Object reqObjec) {

        ObjectOutputStream outputStream = null;

        ObjectInputStream inputStream = null;

        Object object = null;
        try {
            Socket socket = new Socket(host, port);
            outputStream = new ObjectOutputStream(socket.getOutputStream());


            outputStream.writeObject(reqObjec);

            outputStream.flush();
            inputStream = new ObjectInputStream(socket.getInputStream());

            object = inputStream.readObject();


        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return object;
    }
}
